package com.example.demo.service.impl;



import com.example.demo.entity.Assignment;
import com.example.demo.mapper.AssignmentMapper;
import com.example.demo.service.AssignmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AssignmentServiceImpl implements AssignmentService {

    @Autowired
    private AssignmentMapper assignmentMapper;

    @Override
    @Transactional
    public Assignment createAssignment(Assignment assignment) {
        assignmentMapper.insert(assignment);
        return assignment;
    }

    @Override
    public Assignment getAssignmentById(Long id) {
        return assignmentMapper.selectById(id);
    }

    @Override
    public List<Assignment> getAssignmentsByCourseId(Long courseId) {
        return assignmentMapper.selectByCourseId(courseId);
    }

    @Override
    public List<Assignment> getAllAssignments() {
        return assignmentMapper.selectAll();
    }

    @Override
    @Transactional
    public Assignment updateAssignment(Assignment assignment) {
        assignmentMapper.update(assignment);
        return assignmentMapper.selectById(assignment.getId());
    }

    @Override
    @Transactional
    public boolean deleteAssignment(Long id) {
        return assignmentMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public boolean deleteAssignmentsByCourseId(Long courseId) {
        return assignmentMapper.deleteByCourseId(courseId) > 0;
    }

    @Override
    public List<Assignment> getActiveAssignments() {
        return assignmentMapper.selectActiveAssignments();
    }

    @Override
    public int countAssignmentsByCourseId(Long courseId) {
        return assignmentMapper.countByCourseId(courseId);
    }
}